package com.yiya.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiya.bean.PriceRel;
import com.yiya.bean.SysUser;
import com.yiya.mapper.PriceRelMapper;
import com.yiya.model.PriceRelCplex;
import com.yiya.model.PriceRelCplexModel;

@Service("priceRelService")
public class PriceRelService {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(PriceRelService.class);

	@Autowired
	private PriceRelMapper priceRelMapper;
	
	
	public List<PriceRelCplex> queryByList(PriceRelCplexModel bean) {
		Integer rowCount = queryByCount(bean);
		bean.getPager().setRowCount(rowCount);
		List<PriceRelCplex> priceRels = priceRelMapper.selectByModel(bean);
		return priceRels;
	}
	
	public int queryByCount(PriceRelCplexModel bean){
		return priceRelMapper.countByModel(bean);
	}
	
	public int add(PriceRel bean){
		return priceRelMapper.insert(bean);
	}
	
	public int Update(PriceRel bean){
		return priceRelMapper.updateByPrimaryKeySelective(bean);
	}
	
	public PriceRel queryById(Integer id){
		return priceRelMapper.selectByPrimaryKey(id);
	}
	
	public void delete(Integer[] ids,SysUser user){
		Date nowTime = new Date();
		for(Integer id:ids){
			PriceRel bean = priceRelMapper.selectByPrimaryKey(id);
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			bean.setDelFlag(1);
			priceRelMapper.updateByPrimaryKeySelective(bean);
		}
	}
	
	public PriceRel getByMhtAndProd(PriceRel bean){
		/*
		PriceRelExample example = new PriceRelExample();
		example.createCriteria().andMerchantIdEqualTo(bean.getMerchantId()).andProductIdEqualTo(bean.getProductId());
		List<PriceRel> priceRels = priceRelMapper.selectByExample(example);
		PriceRel priceRel = null; 
		if(priceRels!=null){
			priceRel = priceRels.get(0);
		}
		*/
		List<PriceRel> priceRels = priceRelMapper.getByMhtAndProd(bean);
		PriceRel priceRel = null; 
		if(priceRels!=null&&priceRels.size()!=0){
			priceRel = priceRels.get(0);
		}
		return priceRel;
	}

}
